import { Injectable } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { Observable } from 'rxjs';
import { ApiPagedData } from 'src/app/project/model/common/api-paged-data';
import { StudentDTO } from 'src/app/project/model/icm/icm-system-user/student/student.dto';
import { ApiSimpleData } from 'src/app/project/model/common/api-simple-data';

@Injectable({
  providedIn: 'root',
})
export class StudentService {
  private static URL = '/api/icm-student';

  constructor(private http: _HttpClient) {}

  /**
   * 分页查询学生信息
   * @param option 分页参数
   * @param code 学生学号
   * @param name 学生名称
   * @param grade 年级，即入学年份
   * @param academyId 所属学院id
   * @param professionId 所属专业
   * @param classId 所属班级
   */
  findOnePage(
    option: QueryOptions,
    code?: string,
    name?: string,
    grade?: string,
    academyId?: string,
    professionId?: string,
    classId?: string,
  ): Observable<ApiPagedData<StudentDTO>> {
    const params = {};
    Object.assign(
      params,
      option,
      code ? { code } : {},
      name ? { name } : {},
      grade ? { grade } : {},
      academyId ? { academyId } : {},
      professionId ? { professionId } : {},
      classId ? { classId } : {},
    );
    return this.http.get<ApiPagedData<StudentDTO>>(StudentService.URL, params);
  }

  /**
   * 添加学生
   * @param studentDTO 学生信息
   */
  create(studentDTO: StudentDTO): Observable<StudentDTO> {
    return this.http.post<StudentDTO>(StudentService.URL, studentDTO);
  }

  /**
   * 根据id查找学生
   * @param id 学生id
   */
  findById(id: string): Observable<StudentDTO> {
    return this.http.get<StudentDTO>(`${StudentService.URL}/${id}`);
  }

  /**
   * 修改更新学生信息
   * @param id 学生id
   * @param studentDTO 学生信息
   */
  complete(id: string, studentDTO: StudentDTO): Observable<StudentDTO> {
    return this.http.put<StudentDTO>(`${StudentService.URL}/${id}`, studentDTO);
  }

  /**
   * 根据id删除学生
   * @param id 学生id
   */
  delete(id: string): Observable<StudentDTO> {
    return this.http.delete<StudentDTO>(`${StudentService.URL}/${id}`);
  }

  /**
   * 查找所有学生，可按需求排序
   * @param param 排序参数
   * @param pattern 排序模式: asc/desc
   */
  findAllBySort(param?: string, pattern?: string): Observable<Array<StudentDTO>> {
    const params = {};
    Object.assign(params, param ? { param } : {}, pattern ? { pattern } : {});
    return this.http.get<Array<StudentDTO>>(`${StudentService.URL}/all`, params);
  }

  /**
   * 根据学生code，检查学生code是否已存在
   * @param code 学生code
   */
  checkoutCodeIsExist(code: string): Observable<ApiSimpleData<boolean>> {
    const params = {};
    Object.assign(params, code ? { code } : {});
    return this.http.get<ApiSimpleData<boolean>>(`${StudentService.URL}/checkCodeExist`, params);
  }

  /**
   * 根据userId查找学生
   * @param userId userId
   */
  findByUserId(userId: string): Observable<StudentDTO> {
    return this.http.get<StudentDTO>(`${StudentService.URL}/byUser/${userId}`);
  }

  /**
   * 统计学生数量
   */
  count(): Observable<number> {
    return this.http.get<number>(`${StudentService.URL}/count`);
  }
}
